package com.xiaoming.mojbackendjudgeservice.strategy;

import cn.hutool.json.JSONUtil;
import com.xiaoming.mojbackendmodel.model.codesandbox.JudgeInfo;
import com.xiaoming.mojbackendmodel.model.dto.question.JudgeCase;
import com.xiaoming.mojbackendmodel.model.dto.question.JudgeConfig;
import com.xiaoming.mojbackendmodel.model.entity.Question;
import com.xiaoming.mojbackendmodel.model.enums.JudgeInfoMessageEnum;


import java.util.List;
import java.util.Optional;

/**
 * 默认判题策略
 */
public class JavaLanguageJudgeStrategy implements JudgeStrategy {
    @Override
    public JudgeInfo doJudge(JudgeContext judgeContext) {
        //judgeContext allGet
        JudgeInfo judgeInfo = judgeContext.getJudgeInfo();
        //判断编译失败
        if (judgeInfo != null &&judgeInfo.getMessage()!=null && (judgeInfo.getMessage().equals("编译错误") || judgeInfo.getMessage().equals("超时")))        {
            return judgeInfo;
        }
        System.out.println("judgeInfo"+judgeInfo);
        List<String> inputList = judgeContext.getInputList();
        List<String> outputList = judgeContext.getOutputList();
        Question question = judgeContext.getQuestion();
        List<JudgeCase> judgeCaseList = judgeContext.getJudgeCaseList();

        //获取判题限制信息 构造返回对象
        JudgeInfo judgeInfoResponse=new JudgeInfo();
        Long memory = Optional.ofNullable(judgeInfo.getMemory()).orElse(0L);
        Long time = Optional.ofNullable(judgeInfo.getTime()).orElse(0L);
        judgeInfoResponse.setMemory(memory);
        judgeInfoResponse.setTime(time);

        JudgeInfoMessageEnum judgeInfoMessageEnum=JudgeInfoMessageEnum.ACCEPTED;
        //判断题目限制
        String judgeConfigStr = question.getJudgeConfig();
        JudgeConfig judgeConfig = JSONUtil.toBean(judgeConfigStr, JudgeConfig.class);
        System.out.println("judgeconfig"+judgeConfig);
        //假设Java执行多花费10s
        long JAVA_TIME=1000L;
        if(time-JAVA_TIME>judgeConfig.getTimeLimit())
        {
            judgeInfoMessageEnum=JudgeInfoMessageEnum.TIME_LIMIT_ERROR;
            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
            return judgeInfoResponse;
        }
        if (memory > judgeConfig.getMemoryLimit()) {
            judgeInfoMessageEnum = JudgeInfoMessageEnum.MEMORY_LIMIT_EXCEEDED;
            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
            return judgeInfoResponse;
        }
        //判断输入输出长度是否相同
        if (outputList.size()!=inputList.size())
        {
            judgeInfoMessageEnum= JudgeInfoMessageEnum.WRONG_ANSWER;
            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
            return judgeInfoResponse;
        }
        //依次判断输出和输出用例是否一样
        for (int i = 0; i < judgeCaseList.size(); i++) {
            JudgeCase judgeCase1=judgeCaseList.get(i);
            if(!judgeCase1.getOutput().equals(outputList.get(i)))
            {
                judgeInfoMessageEnum=JudgeInfoMessageEnum.WRONG_ANSWER;
                judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
                return judgeInfoResponse;
            }
        }


        judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
        return judgeInfoResponse;


    }
}
